import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'dart:ui';
import 'package:dio/dio.dart';
// 自定义模块
import '../module/my_appbar.dart';
import '../components/msg_user_item.dart';
import '../module/custome_router.dart';
//页面
import './user_chat.dart';

class MsgUserList extends StatefulWidget {
  _MsgUserListState createState() => _MsgUserListState();
}

class _MsgUserListState extends State<MsgUserList> with AutomaticKeepAliveClientMixin {
  String _title = '消  息';
  List userList = [];
  @override
  void initState(){
    _title = _title;
    userList = userList;
    if (userList.length <= 0) {
      getMsgUserList();
    }
    super.initState();
  }
  @override
  bool get wantKeepAlive => true;

  void getMsgUserList ()  {
    Dio().get(
      'https://hinode.shangning.site/api/user/autopushmsg',
      data: {
        'cs_id' : 1138
      }
    ).then((response) {
      if (response.data['status'] == 1) {
        List _userList = [];
        for (int i = 0;i<response.data['result'].length;i++) {
          Map _userItem = {
            'user': {},
            'tap': false
          };
          _userItem['user'] = {
            'user_id': response.data['result'][i]['user_id'],
            'openid': response.data['result'][i]['openid'],
            'name': response.data['result'][i]['name'],
            'nickname': response.data['result'][i]['nickname'],
            'headimgurl': response.data['result'][i]['headimgurl'],
            'last_butt_time': '${response.data['result'][i]['chatlog'][response.data['result'][i]['chatlog'].length - 1]['send_time']}',
            'last_butt_msg': response.data['result'][i]['chatlog'][response.data['result'][i]['chatlog'].length - 1]['content'],
          };
          _userList..add(_userItem);
        }
        setState(() {
          userList =  _userList;
        });
      }
    });

  }
  /**
   * 渲染列表内容
   */
  Widget renderList () {
    if (userList.length > 0) {
      return ListView.builder(
        padding: EdgeInsets.all(0),
        itemCount: userList.length,
        itemBuilder: (context, index) {
          return GestureDetector(
            child: MsgUserItem(
              userItem: userList[index]['user'],
              tapStatus: userList[index]['tap']
            ),
            onTapDown: (TapDownDetails details) {
              setState((){userList[index]['tap'] = true;});
            },
            onTapUp: (TapUpDetails details) {
              setState((){userList[index]['tap'] = false;});
              Navigator.of(context).push(CustomeRouter(
                UserChat(
                  userId: 15,
                )
              ));
              // Navigator.push(context, MaterialPageRoute(
              //   builder:(context) =>UserChat(
              //     userId: 15
              //   ))
              // );
            },
            onTapCancel: () {
              setState((){userList[index]['tap'] = false;});
            },
          );
          
        },
      );
    } else {
      return Center(
        child: Text('加载中...'),
      );
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          MyAppBar(
            title: _title,
            textColor: Colors.white,
            leftICon: IconButton(
              tooltip: '打开客服界面',
              icon: Icon(
                IconData(0xe7f4, fontFamily: 'iconfont'),
                size: ScreenUtil().setWidth(32),
              ),
              color: Colors.white,
              onPressed: () {
                Scaffold.of(context).openDrawer();
              }
            ),
            rightICon: IconButton(
              tooltip: '问题反馈',
              icon: Icon(
                IconData(0xe645, fontFamily: 'iconfont'),
                size: ScreenUtil().setWidth(34),
              ),
              color: Colors.white,
              onPressed: () {
                print('问题反馈');
              }
            ),
          ),
          Expanded(
            child: Container(
              child: renderList(),
              color: Colors.white,
            )
          )
        ],
      ),
    );
  }
}
